home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / stubs / stubinout.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  9.6 KB  |  460 lines

  1. /*======================================================================
  2.  *
  3.  * FILE:
  4.  * stubinout.c
  5.  *
  6.  * IDENTIFICATION:
  7.  * $Header: /private/postgres/src/rules/stubs/RCS/stubinout.c,v 1.9 1991/02/24 00:47:39 cimarron Exp $
  8.  *
  9.  * DESCRIPTION
  10.  *
  11.  * This file contains the routines that transform a stub
  12.  * record to a string and vice versa.
  13.  * If the stub record is in the 'Prs2Stub' format use the routines
  14.  * 'prs2StubToString' and 'prs2StringToStub'. If they are in the
  15.  * 'Prs2RawStub' format, usre 'stubin' and 'stubout'
  16.  *
  17.  *
  18.  *======================================================================
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <strings.h>
  23. #include "rules/prs2.h"
  24. #include "rules/prs2stub.h"
  25. #include "parser/parse.h"    /* for the AND, NOT, OR */
  26. #include "utils/log.h"
  27. #include "tmp/datum.h"
  28. #include "nodes/primnodes.h"
  29. #include "nodes/primnodes.a.h"
  30.  
  31. extern Const RMakeConst();
  32. extern Param RMakeParam();
  33. extern LispValue StringToPlan();
  34.  
  35. /*-------------------
  36.  * these routines are local to this file.
  37.  */
  38. static char *appendString();
  39. static void skipToken();
  40. static long readLongInt();
  41. static char readChar();
  42. static Name readName();
  43.  
  44. #define ISBLANK(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
  45.  
  46.  
  47. /*--------------------------------------------------------------------
  48.  *
  49.  * stubout
  50.  *
  51.  * Given a "Prs2RawStub" transform it to a string readable by humans
  52.  *
  53.  *--------------------------------------------------------------------
  54.  */
  55. char *
  56. stubout(rawStub)
  57. Prs2RawStub rawStub;
  58. {
  59.     Prs2Stub relstub;
  60.     char *res;
  61.  
  62.     relstub = prs2RawStubToStub(rawStub);
  63.     res = prs2StubToString(relstub);
  64.     prs2FreeStub(relstub, true);
  65.     return(res);
  66. }
  67.  
  68. /*--------------------------------------------------------------------
  69.  *
  70.  * stubin
  71.  *
  72.  * Given a string rerpresentin a rule stub record, recreate
  73.  * the 'Prs2RawStub' struct.
  74.  *
  75.  *--------------------------------------------------------------------
  76.  */
  77. Prs2RawStub
  78. stubin(s)
  79. char *s;
  80. {
  81.     Prs2Stub relstub;
  82.     Prs2RawStub rawStub;
  83.  
  84.     relstub = prs2StringToStub(s);
  85.     rawStub = prs2StubToRawStub(relstub);
  86.     prs2FreeStub(relstub, true);
  87.     return(rawStub);
  88. }
  89.  
  90. /*--------------------------------------------------------------------
  91.  *
  92.  * prs2StubToString
  93.  * 
  94.  * Given a stub record create a string containing a represeantation
  95.  * suitable for printing
  96.  *
  97.  * NOTE: space is allocated for the string. When you don't need it
  98.  * anymore, use 'pfree()'
  99.  *--------------------------------------------------------------------
  100.  */
  101. char *
  102. prs2StubToString(relstub)
  103. Prs2Stub relstub;
  104. {
  105.     
  106.     char *res; 
  107.     char s1[200];
  108.     char *s2;
  109.     int i,j, k;
  110.     int maxLength;
  111.     Prs2OneStub oneStub;
  112.     char *p;
  113.     Size size;
  114.     int l;
  115.  
  116.     /*
  117.      * initialize res to the null string.
  118.      */
  119.     maxLength = 0;
  120.     res = NULL;
  121.  
  122.     /*
  123.      * print the number of stubs...
  124.      */
  125.     sprintf(s1, "(numOfStubs: %d", relstub->numOfStubs);
  126.     res = appendString(res, s1, &maxLength);
  127.  
  128.     for (i=0; i<relstub->numOfStubs; i++) {
  129.     /*
  130.      * print the information for this stub record
  131.      */
  132.     oneStub = relstub->stubRecords[i];
  133.     sprintf(s1, " (ruleId: %ld stubId: %u count: %d lock: ",
  134.         oneStub->ruleId,
  135.         oneStub->stubId,
  136.         oneStub->counter);
  137.     res = appendString(res, s1, &maxLength);
  138.     /*
  139.      * now print the rule lock
  140.      * Enclose it in double quotes, so that it
  141.      * will be easy for `prs2StringToStub' to read it
  142.      */
  143.     s2 = RuleLockToString(oneStub->lock);
  144.     res = appendString(res, "`", &maxLength);
  145.     res = appendString(res, s2, &maxLength);
  146.     res = appendString(res, "' ", &maxLength);
  147.     pfree(s2);
  148.     /*
  149.      * Now print the qualification...
  150.      * NOTE: in order to be able to easily read this
  151.      * qual back (by prs2StringToStub) we
  152.      * enclose it in quotes.
  153.      */
  154.     res = appendString(res, "qual: ", &maxLength);
  155.     s2 = lispOut(oneStub->qualification);
  156.     res = appendString(res, "`", &maxLength);
  157.     res = appendString(res, s2, &maxLength);
  158.     res = appendString(res, "'", &maxLength);
  159.     pfree(s2);
  160.     res = appendString(res, ")", &maxLength); /* end of one stub */
  161.     }
  162.     res = appendString(res, ")", &maxLength);
  163.  
  164.     return(res);
  165. }
  166.  
  167. /*--------------------------------------------------------------------
  168.  *
  169.  * prs2StringToStub
  170.  *
  171.  * GIven a "string" representation of a stub, recreate the stub.
  172.  *--------------------------------------------------------------------
  173.  */
  174. Prs2Stub
  175. prs2StringToStub(s)
  176. char *s;
  177. {
  178.     int i,j,k;
  179.     int numOfStubs;
  180.     Prs2Stub stubs;
  181.     Prs2OneStub oneStub;
  182.     ObjectId ruleId;
  183.     Prs2StubId stubId;
  184.     int count;
  185.     int nQuals;
  186.     Datum constDatum;
  187.     AttributeNumber attrNo;
  188.     int indx;
  189.     long l;
  190.     char c;
  191.     RuleLock lock;
  192.     Size size;
  193.     char *data;
  194.  
  195.  
  196.     /*
  197.      * Create an initially empty rule stub.
  198.      */
  199.     stubs = prs2MakeStub();
  200.  
  201.     /*
  202.      * Now read the number of stubs;
  203.      */
  204.     indx = 0;
  205.     skipToken("(", s, &indx);
  206.     skipToken("numOfStubs:", s, &indx);
  207.     l = readLongInt(s, &indx);
  208.     numOfStubs = (int) l;
  209.  
  210.     for (i=0; i<numOfStubs; i++) {
  211.     /*
  212.      * read one `Prs2OneStub'
  213.      * First read the ruleId, stubId, counter, numQuals
  214.      */
  215.     oneStub = prs2MakeOneStub();    /* allocate space */
  216.     skipToken("(", s, &indx);
  217.     skipToken("ruleId:", s, &indx);
  218.     l = readLongInt(s, &indx);
  219.     oneStub->ruleId = (ObjectId) l;
  220.     skipToken("stubId:", s, &indx);
  221.     l = readLongInt(s, &indx);
  222.     oneStub->stubId = (Prs2StubId) l;
  223.     skipToken("count:", s, &indx);
  224.     l = readLongInt(s, &indx);
  225.     oneStub->counter = (int) l;
  226.     /*
  227.      * now read the lock
  228.      * (remember: it was enclosed in quotes).
  229.      */
  230.     skipToken("lock:", s, &indx);
  231.     skipToken("`", s, &indx);
  232.     oneStub->lock = StringToRuleLock(&s[indx]);
  233.     while (s[indx++] != '\'');
  234.     /*
  235.      * now read the qualification
  236.      * (remember: it was enclosed in quotes).
  237.      */
  238.     skipToken("qual:", s, &indx);
  239.     skipToken("`", s, &indx);
  240.     oneStub->qualification = StringToPlan(&s[indx]);
  241.     while (s[indx++] != '\'');
  242.  
  243.     skipToken(")", s, &indx);    /* end of one stub */
  244.  
  245.     prs2AddOneStub(stubs, oneStub);
  246.  
  247.     }
  248.  
  249.     skipToken(")", s, &indx);
  250.     return(stubs);
  251. }
  252.  
  253. /*====================================================================
  254.  * MORE UTILITY ROUTINES LOCAL TO THIS FILE
  255.  *====================================================================
  256.  */
  257.  
  258. /*--------------------------------------------------------------------
  259.  *
  260.  * appendString
  261.  *
  262.  * append string 's' to the string 'res'. *maxLengthP is the
  263.  * memory size allocated for 'res'. If this is not enough to hold
  264.  * both the old contents of 'res' and the appended string 's',
  265.  * allocate some more memory. 
  266.  * It returns the result of the string concatenation, and
  267.  * if more space has been allocated, *maxLengthP is updated.
  268.  *
  269.  * NOTE: this routine works even if 'res' is NULL.
  270.  */
  271. static char*
  272. appendString(res, s, maxLengthP)
  273. char *res;
  274. char *s;
  275. int *maxLengthP;
  276. {
  277.     char *temp;
  278.  
  279.     /*
  280.      * if res is NULL, allocate some initial space
  281.      */
  282.  
  283.     if (res == NULL) {
  284.     *maxLengthP = 100;
  285.     res = (char *) palloc(*maxLengthP);
  286.     if (res==NULL) {
  287.         elog(WARN, "appendString: Out of memory!");
  288.     }
  289.     res[0] = '\0';
  290.     }
  291.  
  292.     if (strlen(s) + strlen(res) >= *maxLengthP -1) {
  293.     while (strlen(s) + strlen(res) >= *maxLengthP -1) {
  294.         *maxLengthP += 100;
  295.     }
  296.     temp = (char *) palloc(*maxLengthP);
  297.     if (temp==NULL) {
  298.         elog(WARN, "appendString: Out of memory!");
  299.     }
  300.     strcpy(temp, res);
  301.     pfree(res);
  302.     res = temp;
  303.     }
  304.     strcat(res, s);
  305.     return(res);
  306. }
  307.  
  308. /*--------------------------------------------------------------------
  309.  *
  310.  * skipToken
  311.  *
  312.  * read & skip the specified token 'token' from the string that
  313.  * starts at 's[*indx]'. '*indx' is incremented accordingly
  314.  * If there is a mismatch, signal a syntax error.
  315.  */
  316. static void
  317. skipToken(token, s, indx)
  318. char *token;
  319. char *s;
  320. int *indx;
  321. {
  322.     char *t;
  323.  
  324.     /*
  325.      * skip blanks
  326.      */
  327.     while (ISBLANK(s[*indx])) {
  328.     (*indx)++;
  329.     }
  330.  
  331.     t = token;
  332.     while (*t != '\0') {
  333.     if (s[*indx] != *t) {
  334.         elog(WARN,"Syntax error while reading RuleStub, token=%s", token);
  335.     }
  336.     t++;
  337.     (*indx)++;
  338.     }
  339. }
  340.  
  341. /*--------------------------------------------------------------------
  342.  *
  343.  * readLongInt
  344.  *
  345.  * read an (long) integer from the string that
  346.  * starts at 's[*indx]'. '*indx' is incremented accordingly
  347.  */
  348. static long
  349. readLongInt(s, indx)
  350. char *s;
  351. int *indx;
  352. {
  353.     long res;
  354.     int sign;
  355.  
  356.     /*
  357.      * skip blanks
  358.      */
  359.     while (ISBLANK(s[*indx])) {
  360.     (*indx)++;
  361.     }
  362.  
  363.     /*
  364.      * Is this a negative number ?
  365.      */
  366.     sign = 1;
  367.     if (s[*indx] == '-') {
  368.     sign = -1;
  369.     (*indx)++;
  370.     }
  371.  
  372.     /*
  373.      * Now read the number
  374.      */
  375.     res = 0;
  376.     while (s[*indx] <='9' && s[*indx] >= '0') {
  377.     res = 10*res + s[*indx] - '0';
  378.     (*indx)++;
  379.     }
  380.  
  381.     res = sign * res;
  382.  
  383.     return(res);
  384. }
  385.  
  386. /*--------------------------------------------------------------------
  387.  *
  388.  * readChar
  389.  *
  390.  * read the first non blank character from the string that
  391.  * starts at 's[*indx]'. '*indx' is incremented accordingly
  392.  */
  393. static char
  394. readChar(s, indx)
  395. char *s;
  396. int *indx;
  397. {
  398.     char c;
  399.  
  400.     /*
  401.      * skip blanks
  402.      */
  403.     while (ISBLANK(s[*indx])) {
  404.     (*indx)++;
  405.     }
  406.  
  407.     c = s[*indx];
  408.     (*indx)++;
  409.     return(c);
  410. }
  411.  
  412. /*-----------------------------------------------------------------------
  413.  * readName
  414.  *
  415.  * Read a name (i.e. a string at most 16 chars long) enclosed in quotes.
  416.  * Returns a palloced Name.
  417.  * 
  418.  * If the name has embedded quotes they must be escaped with a
  419.  * backslash...
  420.  */
  421. static
  422. Name
  423. readName(s, indx)
  424. char *s;
  425. int *indx;
  426. {
  427.     Name res;
  428.     int i;
  429.     char c, c1;
  430.  
  431.     res = (Name) palloc(sizeof(NameData));
  432.     if (res == NULL) {
  433.     elog(WARN, "readString: Out of memory");
  434.     }
  435.  
  436.     skipToken("\"", s, indx);
  437.  
  438.     i = 0;
  439.     c1 = '\0';
  440.     c = readChar(s, indx);
  441.     while (c!= '"' || (c=='"' && c1 == '\\')) {
  442.     if (i>15) {
  443.         elog(WARN, "readString: Name too large!");
  444.     }
  445.     res->data[i] = c;
  446.     i++;
  447.     c1 = c;
  448.     c = readChar(s, indx);
  449.     }
  450.  
  451.     if(i<=15) {
  452.     res->data[i] = '\0';
  453.     }
  454.  
  455.     return(res);
  456. }
  457.  
  458.  
  459.  
  460.